/*
 * JaLingo, http://jalingo.sourceforge.net/
 *
 * Copyright (c) 2002-2006 Oleksandr Shyshko
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package uwncorrector.client.application.gui.main;


import uwncorrector.centre.gui.resources.Resources;
import uwncorrector.centre.gui.util.BaseGui;
import uwncorrector.centre.gui.util.CardPanel;
import uwncorrector.client.application.gui.actions.Actions;
import uwncorrector.client.application.gui.actions.UserLoginAction;
import uwncorrector.client.application.gui.main.describer.DescriberGui;
import uwncorrector.client.application.gui.main.history.HistoryGui;
import uwncorrector.client.application.gui.main.navigator.NavigatorGui;
import uwncorrector.client.application.gui.main.search.SearchGui;
import uwncorrector.client.application.gui.main.synset.SynsetGui;
import uwncorrector.client.application.model.History;
import uwncorrector.client.application.model.Model;
import uwncorrector.client.application.model.ModelAdapter;
import uwncorrector.client.application.model.Preferences;
import uwncorrector.client.application.util.Gaps;
import uwncorrector.client.engine.IEngine;
import net.infonode.tabbedpanel.TabbedPanel;
import net.infonode.tabbedpanel.theme.ShapedGradientTheme;
import net.infonode.tabbedpanel.titledtab.TitledTab;
import net.infonode.tabbedpanel.titledtab.TitledTabProperties;
import net.infonode.util.Direction;
import uwncorrector.client.synset.BadSynsetManager.UserType;
import uwncorrector.client.synset.POS;

import javax.swing.*;
import java.awt.*;

public class MainPanel extends BaseGui {
    private Model model;

    private JPanel gui;
    private MainToolBar mainToolBar; // TODO convert to var
    private JSplitPane splitPane;
    private JSplitPane splitPane2;
    private SynsetGui synsetGui;

    public MainPanel(Model model, IEngine engine, Actions actions, History history, NavigatorGui navigatorGui, HistoryGui historyGui, SearchGui searchGui, SynsetGui synsetGui, DescriberGui describerGui) {
        this.model = model;
        this.synsetGui = synsetGui;

        gui = new JPanel(new BorderLayout());

        mainToolBar = new MainToolBar(actions);
        gui.add(mainToolBar.getGui(), BorderLayout.NORTH);

        TabbedPanel tp = createTabbedPanel(navigatorGui, historyGui, searchGui);
        splitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, describerGui.getGui(), synsetGui.getGui());

        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tp, splitPane2);
        splitPane.setContinuousLayout(true);
        splitPane.setBorder(null);
        gui.add(splitPane, BorderLayout.CENTER);

        // a hack to redraw contents TODO how to fix?
        tp.setSelectedTab(tp.getTabAt(1));
        tp.setSelectedTab(tp.getTabAt(0));

        model.addApplicationModelListener(new ModelAdapter() {
            public Model getModel() {
                return MainPanel.this.model;
            }
            public void settingsUpdated(Preferences preferences) {
                MainPanel.this.mainToolBar.setMemoryBarVisibility(preferences.isMemoryBarVisible());
            }
            public void update_statistics() {
                String approved_tooltip = "", correct_tooltip = "", incorrect_tooltip = "";
                if (getModel().getUserType() == UserType.USER_GUEST) {
                    approved_tooltip = resources.text("approved_tooltip_guest");
                    correct_tooltip = resources.text("correct_tooltip_guest");
                    incorrect_tooltip = resources.text("incorrect_tooltip_guest");
                }
                else if (getModel().getUserType() == UserType.USER_EDITOR) {
                    approved_tooltip = resources.text("approved_tooltip_editor");
                    correct_tooltip = resources.text("correct_tooltip_editor");
                    incorrect_tooltip = resources.text("incorrect_tooltip_editor");
                }
                else if (getModel().getUserType() == UserType.USER_MODERATOR) {
                    approved_tooltip = resources.text("approved_tooltip_moderator");
                    correct_tooltip = resources.text("correct_tooltip_moderator");
                    incorrect_tooltip = resources.text("incorrect_tooltip_moderator");
                }
                showStat("Accepted", getModel().getStatistics().getUser_approved_synset(), approved_tooltip);
                showStat("Corrected", getModel().getStatistics().getUser_correct_synset(), correct_tooltip);
                showStat("Edited", getModel().getStatistics().getUser_incorrect_synset(), incorrect_tooltip);
            }
            private void showStat(String key, int value, String toolTip) {
                getTextField(key).setText(String.valueOf(value));
                getTextField(key).setToolTipText(toolTip);
                getLabel(key).setVisible(true);
                getTextField(key).setVisible(true);
            }
            private void hideStat(String key) {
                getTextField(key).setText("0");
                getLabel(key).setVisible(false);
                getTextField(key).setVisible(false);
            }
            private JLabel getLabel(String key) {
                JPanel panel = getPanel(key);
                if (panel != null)
                    return (JLabel)panel.getComponent(0);
                return null;
            }
            private JTextField getTextField(String key) {
                JPanel panel = getPanel(key);
                if (panel != null)
                    return (JTextField)panel.getComponent(1);
                return null;
            }
            private JPanel getPanel(String key) {
                for (Component comp : MainPanel.this.mainToolBar.getGui().getComponents())
                    if (comp instanceof JPanel)
                        if (((JPanel)comp).getName().contains(key))
                            return (JPanel)comp;
                return null;
            }
            public void login_success(String login) {
                set_icon_to_login_button("logined");
            }
            public void logout() {
                set_icon_to_login_button("action");
                hideStat("Accepted");
                hideStat("Corrected");
                hideStat("Edited");
            }
            private void set_icon_to_login_button(String icoName) {
                for (Component comp : MainPanel.this.mainToolBar.getGui().getComponents())
                    if (comp instanceof JButton) {
                        JButton button = (JButton)comp;
                        if (button.getName().contains("Login")) {
                            button.setIcon(Resources.forProperties(UserLoginAction.class).icon(icoName));
                            break;
                        }
                    }
            }
            public POS get_menu_pos() {
                JComboBox cmbBox = getComboBox();
                if (cmbBox != null)
                    return POS.getPartOfSpeech((String)cmbBox.getSelectedItem());
                return null;
            }
            private JComboBox getComboBox() {
                for (Component comp : MainPanel.this.mainToolBar.getGui().getComponents())
                    if (comp instanceof JComboBox)
                        return (JComboBox)comp;
                return null;
            }
        });
    }


    private TabbedPanel createTabbedPanel(NavigatorGui navigatorGui, HistoryGui historyGui, SearchGui searchGui) {
        ShapedGradientTheme theme = new ShapedGradientTheme();

        TabbedPanel tp = new TabbedPanel();
        tp.getProperties().setTabAreaOrientation(Direction.LEFT);
        tp.getProperties().addSuperObject(theme.getTabbedPanelProperties());

        tp.addTab(createTab("articles", navigatorGui.getGui(), theme.getTitledTabProperties()));
        tp.addTab(createTab("history", historyGui.getGui(), theme.getTitledTabProperties()));
        tp.addTab(createTab("search", searchGui.getGui(), theme.getTitledTabProperties()));

        return tp;
    }

    private TitledTab createTab( String key, JComponent content, TitledTabProperties properties ) {
        content.setBorder( BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) );

        TitledTab tab = new TitledTab( resources.text( key ), resources.icon( key ), content, null );
        tab.getProperties().getNormalProperties().setDirection( Direction.UP );
        tab.getProperties().addSuperObject( properties );
        return tab;
    }

    public JComponent getGui() {
        return gui;
    }

    public int getNavigatorDividerLocation() {
        return splitPane.getDividerLocation();
    }
    public void setNavigatorDividerLocation(int dividerLocation) {
        splitPane.setDividerLocation(dividerLocation);
    }
    public int getMainPanelDividerLocation() {
        return splitPane2.getDividerLocation();
    }
    public void setMainPanelDividerLocation(int dividerLocation) {
        splitPane2.setDividerLocation(dividerLocation);
    }
}
